package com.iyundao.io.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iyundao.io.Service.GiftService;
import com.iyundao.io.mapper.GiftCustomMapper;
import com.iyundao.io.mapper.GiftMapper;
import com.iyundao.io.mapper.SysDeptCustomMapper;
import com.iyundao.io.model.Gift;
import com.iyundao.io.model.SysUserProvider;
import com.iyundao.io.tools.ResponseData;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GiftServiceImpl implements GiftService {

    @Autowired
    private GiftMapper giftMapper;

    @Autowired
    private GiftCustomMapper giftCustomMapper;

    @Autowired
    private SysDeptCustomMapper deptCustomMapper;

    /**
     * 修改礼品状态
     * @param id
     *      礼品id
     * @return
     */
    @Override
    public ResponseData updateFlag(Integer flag,Integer id) {
        int i = giftMapper.updateFlag(flag,id);
        if (i > 0) {
            return new ResponseData(200, "修改成功");
        } else {
            return new ResponseData(0,"修改失败");
        }
    }

    @Override
    public ResponseData insert(Gift gift) {
        int i = giftMapper.insert(gift);
        if (i > 0) {
            return new ResponseData(200, "添加成功");
        } else {
            return new ResponseData(0,"添加失败");
        }
    }

    /**
     * 根据组织id查询组织下所有的礼品
     * @param pagenum
     *      页数
     * @param pagesize
     *      每页显示条数
     * @param organizationid
     *      组织id
     * @return
     */
    @Override
    public PageInfo<Gift> selectByOrganizationId(Integer pagenum, Integer pagesize, String organizationid) {
        List<Gift> list = giftMapper.selectByOrganizationId(organizationid);
        PageHelper.startPage(pagenum,pagesize);
        PageInfo<Gift> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public ResponseData selectById(Integer id) {
        Gift gift = null;
        gift = giftMapper.selectById(id);
        if (gift.equals("") || gift == null) {
            return new ResponseData(0, "未查询到数据");
        } else {
            return new ResponseData(200, "成功", gift);
        }
    }

    @Override
    public ResponseData updateGift(Gift gift) {
        int i = giftMapper.updateSelective(gift);
        if (i > 0) {
            return new ResponseData(200, "修改成功");
        } else {
            return new ResponseData(0, "修改失败");
        }
    }

    /**
     * 模糊查询礼品
     * @param giftname
     *      礼品名称
     * @param organizationid
     *      组织id
     * @return
     */
    @Override
    public PageInfo<Gift> selectByLike(Integer pagenum, Integer pagesize,String giftname, String organizationid) {
        String s = "%" + giftname + "%";
        PageHelper.startPage(pagenum,pagesize);
        List<Gift> list = giftCustomMapper.selectByLike(s, organizationid);
        PageInfo<Gift> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查询用户的社区级别礼品
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public PageInfo<Gift> selectAllBySid(Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        List<Gift> list = null;
        int i = deptCustomMapper.selectChild(sysUser.getSysDept().getSid());
        if (i > 0) {
            list = giftMapper.selectByOrganizationId(sysUser.getSysDept().getSid());
        } else {
            String sid = deptCustomMapper.selectParent(sysUser.getSysDept().getSid());
            list = giftMapper.selectByOrganizationId(sid);
        }
        PageInfo<Gift> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 手机端模糊查询
     * @param pagenum
     * @param pagesize
     * @param giftname
     * @return
     */
    @Override
    public PageInfo<Gift> search(Integer pagenum, Integer pagesize, String giftname) {
        String s = "%" + giftname + "%";
        PageHelper.startPage(pagenum,pagesize);
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        int i = deptCustomMapper.selectChild(sysUser.getSysDept().getSid());
        List<Gift> list = null;
        if (i > 0) {
            list = giftCustomMapper.selectByLikeAndFlag(s,sysUser.getSysDept().getSid());
        } else {
            String sid = deptCustomMapper.selectParent(sysUser.getSysDept().getSid());
            list = giftCustomMapper.selectByLikeAndFlag(s,sid);
        }
        PageInfo<Gift> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public ResponseData deleteGift(Integer id) {
        giftCustomMapper.deleteGift(id);
        giftCustomMapper.deleteGiftImage(id);
        return new ResponseData(200,"成功");
    }
}
